package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import org.springframework.context.ApplicationContext;
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTiet;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTietService;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;

import com.lb.jwitter.service.TblDKMHDotDKNhom;
import com.lb.jwitter.service.TblDKMHDotDKNhomService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHDotDKNhomtblDKMHDotDKHocVienChiTietInsertAction{
    private int pageSizeTblDKMHDotDKHocVienChiTiet; 
    public int getPageSizeTblDKMHDotDKHocVienChiTiet() {
        return pageSizeTblDKMHDotDKHocVienChiTiet;
 }

    public void setPageSizeTblDKMHDotDKHocVienChiTiet(int pageSizeTblDKMHDotDKHocVienChiTiet) {        this.pageSizeTblDKMHDotDKHocVienChiTiet = pageSizeTblDKMHDotDKHocVienChiTiet; }

    private int pageTblDKMHDotDKHocVienChiTiet; 
    public int getPageTblDKMHDotDKHocVienChiTiet() { return pageTblDKMHDotDKHocVienChiTiet; }

    public void setPageTblDKMHDotDKHocVienChiTiet(int pageTblDKMHDotDKHocVienChiTiet) { this.pageTblDKMHDotDKHocVienChiTiet = pageTblDKMHDotDKHocVienChiTiet; }

    private int pagesTblDKMHDotDKHocVienChiTiet; 
    public int getPagesTblDKMHDotDKHocVienChiTiet() { return pagesTblDKMHDotDKHocVienChiTiet; }

    public void setPagesTblDKMHDotDKHocVienChiTiet(int pagesTblDKMHDotDKHocVienChiTiet) { this.pagesTblDKMHDotDKHocVienChiTiet = pagesTblDKMHDotDKHocVienChiTiet; }

    private String dataStringTblDKMHDotDKHocVienChiTiet; 
    public String getDataStringTblDKMHDotDKHocVienChiTiet() { return dataStringTblDKMHDotDKHocVienChiTiet; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private int dotDKHocVienId; 
    public void setDotDKHocVienId(int dotDKHocVienId) {
       this.dotDKHocVienId = dotDKHocVienId;
 }

    public int getDotDKHocVienId() {
       return dotDKHocVienId;
 }

    private TblDKMHDotDKHocVienService tblDKMHDotDKHocVienService; 

    private String ngayBatDau; 
    public void setNgayBatDau(String ngayBatDau) {
       this.ngayBatDau = ngayBatDau;
 }

    public String getNgayBatDau() {
       return ngayBatDau;
 }

    private String ngayKetThuc; 
    public void setNgayKetThuc(String ngayKetThuc) {
       this.ngayKetThuc = ngayKetThuc;
 }

    public String getNgayKetThuc() {
       return ngayKetThuc;
 }

    private String quyen; 
    public void setQuyen(String quyen) {
       this.quyen = quyen;
 }

    public String getQuyen() {
       return quyen;
 }

    private int nhomDKId; 
    public void setNhomDKId(int nhomDKId) {
       this.nhomDKId = nhomDKId;
 }

    public int getNhomDKId() {
       return nhomDKId;
 }

    private TblDKMHDotDKNhomService tblDKMHDotDKNhomService; 

    private TblDKMHDotDKHocVienChiTiet tblDKMHDotDKHocVienChiTiet; 
    public TblDKMHDotDKHocVienChiTiet  getTblDKMHDotDKHocVienChiTiet() {
        return tblDKMHDotDKHocVienChiTiet;
 }

    private List<TblDKMHDotDKHocVienChiTiet> tblDKMHDotDKHocVienChiTiets; 
    public List<TblDKMHDotDKHocVienChiTiet>  getTblDKMHDotDKHocVienChiTiets() {
        return tblDKMHDotDKHocVienChiTiets; 
      } 

    private TblDKMHDotDKHocVienChiTietService tblDKMHDotDKHocVienChiTietService; 

	private int preInsert(Message msg, TblDKMHDotDKHocVienChiTiet obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblDKMHDotDKHocVienChiTiet obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK_HocVien_ChiTiet", "them")){ 
        return "noprivilege"; 
    }
        tblDKMHDotDKHocVienChiTietService = (TblDKMHDotDKHocVienChiTietService) context.getBean("tbl_DKMH_DotDK_HocVien_ChiTietClient"); 
        tblDKMHDotDKHocVienService = (TblDKMHDotDKHocVienService) context.getBean("tbl_DKMH_DotDK_HocVienClient"); 

        tblDKMHDotDKNhomService = (TblDKMHDotDKNhomService) context.getBean("tbl_DKMH_DotDK_NhomClient"); 

        msg = new Message(); 
        tblDKMHDotDKHocVienChiTiet = new TblDKMHDotDKHocVienChiTiet(); 
        TblDKMHDotDKHocVien iTblDKMHDotDKHocVien = new TblDKMHDotDKHocVien(); 
        iTblDKMHDotDKHocVien.setId(new Long(dotDKHocVienId));  
        tblDKMHDotDKHocVienChiTiet.setDotDKHocVienId(dotDKHocVienId <=0 ? null : iTblDKMHDotDKHocVien );  
       try{ 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
       Date dd = df.parse(ngayBatDau); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKHocVienChiTiet.setNgayBatDau(d); 
}catch(Exception e){  e.printStackTrace(); } 
        try{ 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
       Date dd = df.parse(ngayKetThuc); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDKHocVienChiTiet.setNgayKetThuc(d); 
}catch(Exception e){  e.printStackTrace(); } 
         tblDKMHDotDKHocVienChiTiet.setQuyen(quyen); 
        TblDKMHDotDKNhom iTblDKMHDotDKNhom = new TblDKMHDotDKNhom(); 
        iTblDKMHDotDKNhom.setId(new Long(nhomDKId));  
        tblDKMHDotDKHocVienChiTiet.setNhomDKId(nhomDKId <=0 ? null : iTblDKMHDotDKNhom );  
        preInsert(msg,tblDKMHDotDKHocVienChiTiet); 
       if (msg.isPreMessage_b()) 
       { 
           tblDKMHDotDKHocVienChiTietService.createtblDKMHDotDKHocVienChiTiet(tblDKMHDotDKHocVienChiTiet); 
           postInsert(msg, tblDKMHDotDKHocVienChiTiet); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       if(pageSizeTblDKMHDotDKHocVienChiTiet == 0){ 
           pageSizeTblDKMHDotDKHocVienChiTiet = 20; 
       } 
        if(pageTblDKMHDotDKHocVienChiTiet == 0){ 
           pageTblDKMHDotDKHocVienChiTiet =1; 
       } 
        List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
        ConditionReport con = new ConditionReport(); 
        con.setCol("nhomDKId.id"); 
        con.setValue1(new Long(nhomDKId)); 
        con.setOperator("eq"); 
        cons.add(con); 
      tblDKMHDotDKHocVienChiTiets =       tblDKMHDotDKHocVienChiTietService.findtblDKMHDotDKHocVienChiTietByCondition(cons,pageSizeTblDKMHDotDKHocVienChiTiet, pageTblDKMHDotDKHocVienChiTiet); 
      pagesTblDKMHDotDKHocVienChiTiet = tblDKMHDotDKHocVienChiTietService.counttblDKMHDotDKHocVienChiTietByCondition(cons)/pageSizeTblDKMHDotDKHocVienChiTiet + 1; 
      dataStringTblDKMHDotDKHocVienChiTiet = ""; 
      dataStringTblDKMHDotDKHocVienChiTiet += "&id="; 
      dataStringTblDKMHDotDKHocVienChiTiet += "&dotDKHocVienId="; 
      dataStringTblDKMHDotDKHocVienChiTiet += "&ngayBatDau="; 
      dataStringTblDKMHDotDKHocVienChiTiet += "&ngayKetThuc="; 
      dataStringTblDKMHDotDKHocVienChiTiet += "&quyen="; 
      dataStringTblDKMHDotDKHocVienChiTiet += "&nhomDKId="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_DKMH_DotDK_HocVien_ChiTiet Action"); 
         e.printStackTrace(); 
         msg.setPostMessage_b(false); 
         msg.setPostMessage_s(e.getMessage()); 
         return Action.ERROR; 
 } 
 } 
 } 
 
